package com.banciyuan.bcywebview.biz.write.write;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.model.PhotoModel;
import com.banciyuan.bcywebview.biz.write.photoselecotor.pselector.PhotoActivity;
import com.banciyuan.bcywebview.utils.file.ImageManager;

import java.io.File;
import java.io.Serializable;
import java.util.List;

public class WriteGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<PhotoModel> plist;
    private HeaderHolder headerHolder;
    private LruCache<String, Bitmap> cache = new LruCache<>(9);

    public WriteGridAdapter(Context mContext, HeaderHolder headerHolder, List<PhotoModel> plist) {
        this.mContext = mContext;
        this.plist = plist;
        this.headerHolder = headerHolder;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return headerHolder;
            case 1:
                return new ImageHolder(mInflater.inflate(R.layout.add_group_grid_item, null));
            default:
                return new ImageHolder(mInflater.inflate(R.layout.add_group_grid_item, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 1:
                ImageHolder imageHolder = (ImageHolder) holder;
                setImage(position - 1, imageHolder);
                break;
        }
    }

    private void setImage(final int position, final ImageHolder imageHolder) {
        if (position >= plist.size()) {
            imageHolder.content.setImageResource(R.drawable.publish_add);
            imageHolder.progress.setVisibility(View.GONE);
            imageHolder.delete.setVisibility(View.GONE);
        } else {
            if (plist.get(position) != null) {
                String key = plist.get(position).getOriginalPath();
                if (!TextUtils.isEmpty(key)) {
                    new AsyncTask<Void, Bitmap, Bitmap>() {
                        @Override
                        protected Bitmap doInBackground(Void... params) {
                            try {
                                File mFile = new File(plist.get(position).getOriginalPath());
                                Bitmap bitmap = ImageManager.getTempImage(4, mFile.getAbsolutePath());
                                cache.put(plist.get(position).getOriginalPath(), bitmap);
                                return bitmap;
                            } catch (Exception e) {
                                return null;
                            }
                        }

                        @Override
                        protected void onPostExecute(Bitmap bitmap) {
                            if (bitmap != null) {
                                imageHolder.content.setImageBitmap(bitmap);
                                imageHolder.delete.setVisibility(View.VISIBLE);
                                setProgress(plist.get(position), imageHolder);
                            }
                        }
                    }.execute();
                }
            } else {
                String key = plist.get(position).getOriginalPath();
                if (!TextUtils.isEmpty(key)) {
                    imageHolder.content.setImageBitmap(cache.get(plist.get(position).getOriginalPath()));
                    imageHolder.delete.setVisibility(View.VISIBLE);
                    setProgress(plist.get(position), imageHolder);
                }
            }
        }

        OnClick gridListener = new OnClick(position);
        imageHolder.content.setOnClickListener(gridListener);
        imageHolder.delete.setOnClickListener(gridListener);
    }

    private void setProgress(PhotoModel p, ImageHolder imageHolder) {
        if (!p.isUpload()) {
            imageHolder.progress.setVisibility(View.VISIBLE);
            imageHolder.progress.setText(String.valueOf((int) (p.getProgress() * 100)) + "%");
        } else {
            imageHolder.progress.setVisibility(View.GONE);
        }
    }

    @Override
    public long getItemId(int postion) {
        return postion;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        if (plist.size() > 0 && plist.size() < 9) {
            return plist.size() + 2;
        } else {
            return plist.size() + 1;
        }
    }

    private class ImageHolder extends RecyclerView.ViewHolder {
        ImageView content;
        ImageView delete;
        TextView progress;

        public ImageHolder(View itemView) {
            super(itemView);
            delete = (ImageView) itemView.findViewById(R.id.item_group_cancel);
            content = (ImageView) itemView.findViewById(R.id.item_group_content);
            progress = (TextView) itemView.findViewById(R.id.progress);
        }
    }

    class OnClick implements OnClickListener {

        int position;

        public OnClick(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.item_group_cancel:
                    if (!plist.isEmpty() && position < plist.size()) {
                        plist.remove(position);
                    }
                    notifyDataSetChanged();
                    break;
                case R.id.item_group_content:
                    if (position == plist.size()) {
                        startImagePic();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    private void startImagePic() {
        Intent intent = new Intent(mContext, PhotoActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("photos", (Serializable) plist);
        intent.putExtras(bundle);
        ((Activity) mContext).startActivityForResult(intent, WriteHelper.PICK_IMAGE);
    }
}